package com.sc.nft.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.controller.vo.DaoIdentityVO;
import com.sc.nft.dao.DaoIdentityDao;
import com.sc.nft.dao.DigitalCollectionProductInfoDao;
import com.sc.nft.dao.UserCollectionDao;
import com.sc.nft.entity.DaoIdentity;
import com.sc.nft.entity.UserCreatorInfo;
import com.sc.nft.entity.dto.HomeBrandIslandDTO;
import com.sc.nft.entity.dto.UserApplyCreatedDaoDTO;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.entity.vo.GetUserApplyCreateDaoVO;
import com.sc.nft.entity.vo.GetWhatHeSSellingUnderTheCollectionVO;
import com.sc.nft.entity.vo.HomeBrandIslandVO;
import com.sc.nft.enums.DaoLevelTypeEnum;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.UserCreatorInfoService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/userCreatorInfo")
@Api(tags = "用户dao")
public class UserCreatorInfoController {

    private final UserCreatorInfoService userCreatorInfoService;
    private final DaoIdentityDao daoIdentityDao;
    private final DigitalCollectionProductInfoDao productInfoDao;

    private final UserCollectionDao userCollectionDao;


    @ApiOperation(value = "用户申请创建Dao", notes = "用户申请创建Dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userApplyCreatedDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result userApplyCreatedDao(@RequestBody UserApplyCreatedDaoDTO userApplyCreatedDaoDTO) {
        userCreatorInfoService.userApplyCreatedDao(userApplyCreatedDaoDTO);
        return Result.success();
    }

    @ApiOperation(value = "用户自己创建的dao列表", notes = "用户申请创建Dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getUserApplyCreateDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<GetUserApplyCreateDaoVO> getUserApplyCreateDao(@RequestBody PageRequest pageRequest) {
        return PageResult.success(userCreatorInfoService.getUserApplyCreateDaoVO(pageRequest));
    }

    @ApiOperation(value = "用户加入的群组", notes = "用户加入的群组", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userJoinGroup", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<GetUserApplyCreateDaoVO> userJoinGroup(@RequestBody PageRequest pageRequest) {
        return PageResult.success(userCreatorInfoService.userJoinGroup(pageRequest));
    }

    @ApiOperation(value = "dao身份列表", notes = "创建dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/daoIdentityList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<DaoIdentityVO>> daoIdentityList(@Valid @RequestBody IdRequest request) {
        UserCreatorInfo userCreatorInfo = userCreatorInfoService.getById(request.getId());
        if(ObjectUtil.isNull(userCreatorInfo)||userCreatorInfo.getStatus().intValue()!=1){
            return Result.fail("DAO不存在");
        }
            List<DaoIdentity> daoIdentityList = daoIdentityDao.getByDaoId(request.getId());
        //找到身份等级最高的身份卡
//        DaoIdentity maxDaoIdentity = daoIdentityList.stream().max(Comparator.comparing(DaoIdentity::getLevel)).get();
        List<DaoIdentityVO> result = Lists.newArrayList();
        for (DaoIdentity daoIdentity : daoIdentityList) {
            DaoIdentityVO vo = new DaoIdentityVO();
            vo.setId(daoIdentity.getId());
            vo.setIdentityName(daoIdentity.getIdentityName());
            vo.setIdentityIcon(daoIdentity.getIdentityIcon());
            vo.setEquityImage(daoIdentity.getEquityImage());
            vo.setCollectionId(daoIdentity.getCollectionId());
            vo.setIsActivate(userCollectionDao.userHasCollection(UserPool.getUser().getId(),daoIdentity.getCollectionId()));
            // 获取当前藏品id是否有上架的商品
            vo.setStatus(productInfoDao.isExist(daoIdentity.getCollectionId()));
            result.add(vo);
        }


        return Result.success(result);
    }

    @ApiOperation(value = "根据藏品id获取他下面在售的商品", notes = "根据藏品id获取他下面在售的商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getWhatHeSSellingUnderTheCollectionId", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<GetWhatHeSSellingUnderTheCollectionVO>> getWhatHeSSellingUnderTheCollectionId(@Valid @RequestBody IdRequest request) {
        List<GetWhatHeSSellingUnderTheCollectionVO> result = userCreatorInfoService.getWhatHeSSellingUnderTheCollectionId(request.getId());
        return Result.success(result);
    }

    @ApiOperation(value = "获取用户是否有城主卡可以展示我要发起功能", notes = "用户申请创建Dao", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getUserShowApplyCreateDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result getUserShowApplyCreateDao() {
        Map map = new HashMap<>();
        map.put("userShowApplyCreate", UserPool.getUser().getLevel()== DaoLevelTypeEnum.CITY_LORD);
        return Result.success(map);
    }

    @ApiOperation(value = "首页dao品牌岛", notes = "首页dao品牌岛", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/homeBrandIsland", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<HomeBrandIslandVO>> homeBrandIsland() {
        List<HomeBrandIslandVO> homeBrandIsland = userCreatorInfoService.homeBrandIsland();
        return Result.success(homeBrandIsland);
    }

    @ApiOperation(value = "首页dao品牌岛全部", notes = "首页dao品牌岛全部", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/homeBrandIslandPage", produces = MediaType.APPLICATION_JSON_VALUE)
//    @UserLogin
    public PageResult<List<HomeBrandIslandVO>> homeBrandIslandPage(@RequestBody HomeBrandIslandDTO request) {
        Page<HomeBrandIslandVO> homeBrandIsland = userCreatorInfoService.homeBrandIslandPage(request);
        return PageResult.success(homeBrandIsland);
    }

    @ApiOperation(value = "dao主页", notes = "dao主页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getDaoById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<GetDaoByIdVO> getDaoById(@RequestBody IdRequest idRequest) {
        return Result.success(userCreatorInfoService.getDaoById(idRequest.getId()));
    }

    @ApiOperation(value = "获取品牌的Dao模块", notes = "获取品牌的Dao模块", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCertainlyShowDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetDaoByIdVO> getCertainlyShowDao() {
        return Result.success(userCreatorInfoService.getCertainlyShowDao());
    }

}
